package org.springblade.mes.logistics.businessType.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.logistics.businessType.entity.BusinessType;
import org.springblade.mes.logistics.businessType.mapper.BusinessTypeMapper;
import org.springblade.mes.logistics.businessType.model.BusinessTypeBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 服务
 *
 * @Version 1.0
 */
@Slf4j
@Service
@DS("logistics")
public class BusinessTypeService extends BaseServiceImpl<BusinessTypeMapper, BusinessType> {

    @Transactional(rollbackFor = Exception.class)
    public boolean addData(BusinessTypeBean param) throws Exception {
        log.debug("新增检测通过");
        //保存
        BusinessType entity = param;
        return this.save(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateData(BusinessTypeBean param) throws Exception {
        log.debug("修改检测通过");
        //更新
        BusinessType newEntity = param;
        return this.updateById(newEntity);
    }


    @Transactional(rollbackFor = Exception.class)
    public boolean deleteData(BusinessTypeBean param) throws Exception {
        if (param.getIdList() != null && param.getIdList().size() > 0) {
            UpdateWrapper<BusinessType> del = new UpdateWrapper<>();
            del.lambda().in(BusinessType::getId, param.getIdList());
            return this.remove(del);
        }
        if (param.getId() != null) {
            return this.removeById(param.getId());
        }
        return true;
    }


    public BusinessTypeBean findById(Long id) {
        BusinessTypeBean cond = new BusinessTypeBean();
        cond.setId(id);
        return findBySpec(cond);
    }


    public BusinessTypeBean findBySpec(BusinessTypeBean param) {
        List<BusinessTypeBean> list = this.baseMapper.queryList(param);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }


    public List<BusinessTypeBean> findListBySpec(BusinessTypeBean param) {
        return this.baseMapper.queryList(param);
    }


    public IPage<BusinessTypeBean> findPageBySpec(IPage<BusinessTypeBean> page, BusinessTypeBean param) {
        List<BusinessTypeBean> dataList = baseMapper.queryPageList(page, param);
        return page.setRecords(dataList);
    }

    /**
     * 字典赋值
     */
    private List<BusinessTypeBean> fillDict(List<BusinessTypeBean> dataList) {
        if (dataList == null || dataList.size() == 0) {
            return Collections.emptyList();
        }
        for (BusinessTypeBean vo : dataList) {
            //TODO
        }
        return dataList;
    }
}

